/**
 * @param {number[]} pushed
 * @param {number[]} popped
 * @return {boolean}
 */
 var validateStackSequences = function(pushed, popped) {

};

var validateStackSequences = function(pushed, popped) {
	/* 
			解题思路构造一个辅助栈 stack 模拟出栈行为
			1. 入栈操作： 按照压栈序列的顺序执行
			2. 出栈操作： 每次入栈后，循环判断 “栈顶元素 = 弹出序列的当前元素” 是否成立，将符合弹出序列顺序的栈顶元素全部弹出 
	*/
	const stack = [], pop = () => stack[stack.length - 1] 
	let i = 0


	for (const num of pushed) {
			stack.push(num)
			while (stack.length && pop() === popped[i]) {
					stack.pop()
					i++
			}
	}

	return stack.length === 0
};